﻿using AIVision.Model.EntityModels;
using AIVision.Repository.Entitys;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIVision.Repository.Services
{
    public class CheckTempleteService
    {
        private readonly AiVisionDbContext dbContext;
        private readonly IMapper mapper;

        public CheckTempleteService(AiVisionDbContext dbContext, IMapper mapper)
        {
            this.dbContext = dbContext;
            this.mapper = mapper;
        }

        //public CheckTemplateDto GetDefultTemplete()
        //{
        //    CheckTemplateDto ct = new CheckTemplateDto();
        //    ct.Code = "001";
        //    ct.Name="Test";
        //    ct.CheckStations = new ObservableCollection<CheckStationDto>();

        //    CheckStationDto station1 = new CheckStationDto();
        //    station1.Index = 0;
        //    station1.StationCode = "001";
        //    station1.IsSaveOKImage = true;
        //    station1.IsSaveDefectImage = false;
        //    station1.ImageFileSavePath = "E:\\pic";
        //    station1.ImageCache = false;
        //    station1.Camera = new CameraDto() { Code="001",Name="Test1",HardKey= "2BDFA3692625" };
        //    station1.HSmartWindowName = "Hsmart1";
        //    ct.CheckStations.Add(station1);

        //    CheckStationDto station2 = new CheckStationDto();
        //    station2.Index = 1;
        //    station2.StationCode = "002";
        //    station2.IsSaveOKImage = true;
        //    station2.IsSaveDefectImage = false;
        //    station2.ImageFileSavePath = "E:\\pic";
        //   station2.ImageCache = false;
        //   station2.Camera = new CameraDto() { Code = "002", Name = "Test2", HardKey = "2BDFA3692620" };
        //    station2.HSmartWindowName = "Hsmart2";
        //    ct.CheckStations.Add(station2);


        //    CheckStationDto station3 = new CheckStationDto();
        //    station3.Index = 1;
        //    station3.StationCode = "003";
        //    station3.IsSaveOKImage = true;
        //    station3.IsSaveDefectImage = false;
        //    station3.ImageFileSavePath = "E:\\pic";
        //    station3.ImageCache = false;
        //    station3.Camera = new CameraDto() { Code = "003", Name = "Test3", HardKey = "2BDFA3692637" };
        //    station3.HSmartWindowName = "Hsmart3";
        //    ct.CheckStations.Add(station3);


        //    CheckStationDto station4 = new CheckStationDto();
        //    station4.Index = 1;
        //    station4.StationCode = "004";
        //    station4.IsSaveOKImage = true;
        //    station4.IsSaveDefectImage = false;
        //    station4.ImageFileSavePath = "E:\\pic";
        //    station4.ImageCache = false;
        //    station4.Camera = new CameraDto() { Code = "004", Name = "Test4", HardKey = "2BDFA3692622" };
        //    station4.HSmartWindowName = "Hsmart4";
        //    ct.CheckStations.Add(station4);

        //    return ct;


        //}


        public ObservableCollection<CheckTemplateDto>  GetAllTempleteList()
        {
            ObservableCollection<CheckTemplateDto> result = new ObservableCollection<CheckTemplateDto>();

           var entitys = dbContext.CheckTemplates.ToList();
            foreach (var item in entitys)
            {
                result.Add(mapper.Map<CheckTemplateDto>(item));
            }
            return result;
        }
        public ObservableCollection<CheckStationDto> GetStationsByTemplete(CheckTemplateDto templete)
        {
            ObservableCollection<CheckStationDto> result = new ObservableCollection<CheckStationDto>();

            var entitys = dbContext.CheckStations.Where(m=>m.CheckTemplateId==templete.Id).ToList();
            foreach (var item in entitys)
            {
                result.Add(mapper.Map<CheckStationDto>(item));
            }
            return result;
        }
        public ObservableCollection<DefectItemDto> GetDefectByStation(CheckStationDto station)
        {
            ObservableCollection<DefectItemDto> result = new ObservableCollection<DefectItemDto>();

            var entitys = dbContext.DefectItems.Where(m => m.CheckStationId == station.Id).ToList();
            foreach (var item in entitys)
            {
                result.Add(mapper.Map<DefectItemDto>(item));
            }
            return result;
        }

        public void SaveTempletes(ObservableCollection<CheckTemplateDto> checkTemplates)
        {
            foreach (var item in checkTemplates)
            {
                var newTemplete = mapper.Map<CheckTemplate>(item);
                newTemplete.CheckStations = null;

                var templete = dbContext.CheckTemplates.FirstOrDefault(m => m.Id == item.Id);
                if (templete != null)
                    dbContext.Entry(templete).CurrentValues.SetValues(newTemplete);
                else
                    dbContext.CheckTemplates.Add(newTemplete);
            }
            dbContext.SaveChanges();
        }
        public void SaveStations(ObservableCollection<CheckStationDto> checkStations,int templeteId)
        {
            foreach (var station in checkStations)
            {
                var newStation = mapper.Map<CheckStation>(station);
                newStation.CheckTemplate = null;
                newStation.CheckTemplateId = templeteId;

                var dbStation = dbContext.CheckStations.FirstOrDefault(m => m.Id == station.Id);
                if (dbStation != null)
                    dbContext.Entry(dbStation).CurrentValues.SetValues(newStation);
                else
                    dbContext.CheckStations.Add(newStation);
            }

            dbContext.SaveChanges();
        }
        public void SaveDefects(ObservableCollection<DefectItemDto> defects, int stationId)
        {
            foreach (var defect in defects)
            {
                var newDefect = mapper.Map<DefectItem>(defect);
                newDefect.CheckStation = null;
                newDefect.CheckStationId = stationId;

                var dbDefect = dbContext.DefectItems.FirstOrDefault(m => m.Id == defect.Id);
                if (dbDefect != null)
                    dbContext.Entry(dbDefect).CurrentValues.SetValues(newDefect);
                else
                    dbContext.DefectItems.Add(newDefect);
            }

            dbContext.SaveChanges();
        }
        public void RemoveTemplete(CheckTemplateDto templete)
        {
            var entity = dbContext.CheckTemplates.FirstOrDefault(m => m.Id == templete.Id);
            if (entity != null)
            {
                dbContext.CheckTemplates.Remove(entity);
                dbContext.SaveChanges();
            }
                
        }
        public void RemoveStation(CheckStationDto station)
        {
            var entity = dbContext.CheckStations.FirstOrDefault(m => m.Id == station.Id);
            if (entity != null)
            {
                dbContext.CheckStations.Remove(entity);
                dbContext.SaveChanges();
            }
        }

        public void RemoveDefect(DefectItemDto defect)
        {
            var entity = dbContext.DefectItems.FirstOrDefault(m => m.Id == defect.Id);
            if (entity != null)
            {
                dbContext.DefectItems.Remove(entity);
                dbContext.SaveChanges();
            }
        }
        public CheckTemplateDto GetStartTemplete()
        {

            var entity = dbContext.CheckTemplates.Include(m=>m.CheckStations).ThenInclude(n=>n.Camera)
                    .Include(m => m.CheckStations).ThenInclude(n => n.DefectItems).FirstOrDefault(m => m.IsStart == true);
            if (entity != null)
            {
                return mapper.Map<CheckTemplateDto>(entity);
            }
            return null;

        }

    }
}
